package pers.zxk.jlytnews.dao.impl;

import pers.zxk.jlytnews.dao.WxChannelDao;
import pers.zxk.jlytnews.entity.WxAccountInfo;
import pers.zxk.jlytnews.entity.WxArticle;
import pers.zxk.jlytnews.entity.WxChannel;
import pers.zxk.jlytnews.util.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class WxChannelDaoImpl implements WxChannelDao {
    public static final String TABLE_NAME = "tb_wx_channel";
    public static final String COL_ID = "id";
    public static final String COL_TITLE = "title";
    public static final String COL_FAKE_ID = "fakeid";
    public static final String COL_NICKNAME = "nickname";
    public static final String COL_ROUND_HEAD_IMG = "round_head_img";
    public static final String COL_SORT = "sort";

    @Override
    public boolean addWxChannel(String title, String fakeid, String nickname, String headimg) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        int i = DBconn.addUpdDel("insert into " + TABLE_NAME + "("
                + COL_TITLE + "," + COL_FAKE_ID + "," + COL_NICKNAME + "," + COL_ROUND_HEAD_IMG + ") " +
                "values('" + title + "','" + fakeid + "','" + nickname + "','" + headimg + "')");
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean delWxChannel(int id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from " + TABLE_NAME + " where " + COL_ID + "=" + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateWxChannel(int id, String title, String fakeid, String nickname, String headimg) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set " + COL_TITLE + " ='" + title
                + "' , " + COL_FAKE_ID + " ='" + fakeid
                + "' , " + COL_NICKNAME + " ='" + nickname
                + "' , " + COL_ROUND_HEAD_IMG + " ='" + headimg
                + "' where " + COL_ID + " = " + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public WxChannel getWxChannel(int id) throws SQLException, ClassNotFoundException {
        DBconn.init();
        ResultSet rs = DBconn.selectSql("select * from " + TABLE_NAME + " where " + COL_ID + " = " + id);
        if (rs.next()) {
            WxChannel wxChannel = new WxChannel(id,
                    rs.getString(COL_TITLE),
                    rs.getString(COL_FAKE_ID),
                    rs.getString(COL_NICKNAME),
                    rs.getString(COL_ROUND_HEAD_IMG),
                    rs.getInt(COL_SORT));
            DBconn.closeConn();
            return wxChannel;
        } else {
            DBconn.closeConn();
            throw new SQLException("未找到id为" + id + "的数据");
            //return null;
        }
    }

    @Override
    public List<WxChannel> getWxChannelList() throws SQLException, ClassNotFoundException {
        List<WxChannel> list = new ArrayList<WxChannel>();
        DBconn.init();
        ResultSet rs;

        rs = DBconn.selectSql("select * from " + TABLE_NAME + " order by " + COL_SORT);

        while (rs.next()) {
            WxChannel wxChannel = new WxChannel(rs.getInt(COL_ID),
                    rs.getString(COL_TITLE),
                    rs.getString(COL_FAKE_ID),
                    rs.getString(COL_NICKNAME),
                    rs.getString(COL_ROUND_HEAD_IMG),
                    rs.getInt(COL_SORT));
            list.add(wxChannel);
        }
        DBconn.closeConn();
        return list;
    }

    @Override
    public boolean setSort(int id, int sort) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update " + TABLE_NAME + " set "
                + COL_SORT + " =" + sort
                + " where " + COL_ID + " = " + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateWxAccount(String token, String cookie) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update tb_wx_account set token ='" + token
                + "' , cookie ='" + cookie
                + "' where id = 1";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public WxAccountInfo getWxAccount() throws SQLException, ClassNotFoundException {
        DBconn.init();
        ResultSet rs = DBconn.selectSql("select * from tb_wx_account where id = 1");
        if (rs.next()) {
            WxAccountInfo wxAccountInfo = new WxAccountInfo();
            wxAccountInfo.setId(rs.getInt("id"));
            wxAccountInfo.setToken(rs.getString("token"));
            wxAccountInfo.setCookie(rs.getString("cookie"));
            wxAccountInfo.setUser_agent(rs.getString("user_agent"));
            DBconn.closeConn();
            return wxAccountInfo;
        } else {
            DBconn.closeConn();
            throw new SQLException("未找到数据");
            //return null;
        }
    }
}
